<template>
  <div class="salarys-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <!-- 左侧 -->
          <el-tab-pane label="角色管理">
            <!-- 按钮 -->
            <el-button
              icon="el-icon-plus"
              size="small"
              type="primary"
              @click="add"
              >新增角色</el-button
            >
            <!-- 表格 -->
            <el-table :data="list">
              <el-table-column
                type="index"
                :index="index"
                label="序号"
                width="100"
              />
              <el-table-column prop="name" label="角色名称" width="240" />
              <el-table-column prop="description" label="描述" />
              <el-table-column label="操作">
                <template v-slot="{ row }">
                  <el-button size="small" type="success">分配权限</el-button>
                  <el-button size="small" type="primary" @click="edit(row.id)"
                    >编辑</el-button
                  >
                  <el-button size="small" type="danger" @click="del(row.id)"
                    >删除</el-button
                  >
                </template>
              </el-table-column>
            </el-table>
            <!-- 分页 -->
            <el-pagination
              :current-page="page"
              :page-sizes="[2, 3, 5, 10]"
              :page-size="pagesize"
              layout="total, sizes, prev, pager, next, jumper"
              :total="total"
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
            />
          </el-tab-pane>

          <!-- 公司信息 -->
          <el-tab-pane label="公司信息">
            <!-- 警告信息 -->
            <el-alert
              title="对公司名称、公司地址、营业执照、公司地区的更新，将使得公司资料被重新审核，请谨慎修改"
              type="info"
              show-icon
              :closable="false"
            />
            <!-- 表单 -->
            <el-form label-width="120px" style="margin-top: 50px">
              <el-form-item label="公司名称">
                <el-input
                  v-model="companyInfo.name"
                  disabled
                  style="width: 400px"
                />
              </el-form-item>
              <el-form-item label="公司地址">
                <el-input
                  v-model="companyInfo.companyAddress"
                  disabled
                  style="width: 400px"
                />
              </el-form-item>
              <el-form-item label="邮箱">
                <el-input
                  v-model="companyInfo.mailbox"
                  disabled
                  style="width: 400px"
                />
              </el-form-item>
              <el-form-item label="备注">
                <el-input
                  v-model="companyInfo.remarks"
                  type="textarea"
                  :rows="3"
                  disabled
                  style="width: 400px"
                />
              </el-form-item>
            </el-form>
          </el-tab-pane>
        </el-tabs>
      </el-card>

      <!-- 弹出框 -->
      <el-dialog :title="showTitle" :visible="showDialog" @close="handleClose">
        <el-form
          ref="roleForm"
          :model="form"
          :rules="rules"
          label-width="100px"
        >
          <el-form-item label="角色名称" prop="name">
            <el-input v-model="form.name" placeholder="请输入角色名称" />
          </el-form-item>
          <el-form-item label="角色描述">
            <el-input v-model="form.description" placeholder="请输入角色描述" />
          </el-form-item>
        </el-form>

        <template #footer>
          <el-button @click="handleClose">取消</el-button>
          <el-button type="primary" @click="submitBtn">确认</el-button>
        </template>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { mapState } from 'vuex'
import {
  getRoleList,
  deleteRole,
  addRole,
  updateRole,
  getRoleDetail,
  getCompanyById
} from '@/api/setting'
export default {
  name: 'Setting',
  data() {
    return {
      list: [],
      page: 1,
      pagesize: 10,
      total: 0,
      loading: false,
      showDialog: false,
      form: {
        name: '',
        description: ''
      },
      companyInfo: {},
      rules: {
        name: [
          {
            required: true,
            message: '请输入角色名称',
            trigger: ['blur', 'change']
          }
        ]
      }
    }
  },
  // 控制标题
  computed: {
    ...mapState('user', ['userInfo']),
    showTitle() {
      return this.form.id ? '编辑角色' : '添加角色'
    }
  },
  created() {
    this.getRole()
    this.getCompanyInfo()
  },
  methods: {
    async getRole() {
      this.loading = true
      const { data: res } = await getRoleList(this.page, this.pagesize)
      // console.log(res)
      this.total = res.total
      this.list = res.rows
      this.loading = false
    },
    // 分页处理
    handleSizeChange(newSize) {
      this.pagesize = newSize
      this.getRole()
    },
    handleCurrentChange(newPage) {
      this.page = newPage
      this.getRole()
    },
    // 自定义序号
    index(index) {
      return (this.page - 1) * this.pagesize + index + 1
    },
    // 删除角色
    async del(id) {
      const res = await this.$confirm('确定删除吗', '提示').catch((err) => err)
      if (res === 'cancel') return
      await deleteRole(id)
      this.$message.success('删除成功')
      // 处理每页删除最后一个显示暂无数据的bug
      if (this.list.length === 1 && this.page > 1) {
        this.page--
      }
      this.getRole()
    },
    // 关闭弹出框
    handleClose() {
      this.showDialog = false
      this.$refs.roleForm.resetFields()
      this.form = {
        name: '',
        description: ''
      }
    },
    // 新增角色
    add() {
      this.showDialog = true
    },
    submitBtn() {
      this.$refs.roleForm.validate(async(valid) => {
        if (!valid) return
        if (this.form.id) {
          // 编辑
          await updateRole(this.form)
          this.$message.success('编辑成功')
        } else {
          await addRole(this.form)
          this.$message.success('新增成功')
        }
        this.handleClose()
        this.getRole()
      })
    },
    // 编辑角色
    async edit(id) {
      const res = await getRoleDetail(id)
      // console.log(res)
      this.form = res.data
      this.showDialog = true
    },
    // 获取公司信息
    async getCompanyInfo() {
      const res = await getCompanyById(this.userInfo.companyId)
      this.companyInfo = res.data
    }
  }
}
</script>
<style>
</style>
